package com.prp.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;

import com.prp.dao.Adminrole;
import com.prp.dao.AdminroleDAO;
import com.prp.dao.AdminroleId;
import com.prp.dao.Role;
import com.prp.dao.RoleDAO;
import com.prp.dao.Userinfo;
import com.prp.dao.UserinfoDAO;
import com.prp.interfaces.*;
import com.prp.security.Initialization;

public class AdminFunctionImpl implements IAdminSystem{

	public List getAllUser()
	{
		UserinfoDAO dao = new UserinfoDAO();
		
		return dao.findAll();
	}
	
	
	public Userinfo getUser(String username)
	{
		UserinfoDAO dao = new UserinfoDAO();
		Userinfo user = dao.findById(username);
		
		return user;
	}
	
	public List getAuths(Userinfo user)
	{
		List temp = new ArrayList();
		
		Iterator iter = user.getRoles().iterator();
		while(iter.hasNext())
		{
			Role r = (Role) iter.next();
			temp.add(r.getRole().toString());
		}
		
		
		return temp;
	}
	
	public boolean editUserAuth(String username, boolean isAdmin, List authority) {
		UserinfoDAO userinfoDAO = new UserinfoDAO();
		userinfoDAO.getSession().beginTransaction();
		
		Userinfo temp = userinfoDAO.findById(username);
		
		
		if( temp == null){
			userinfoDAO.getSession().getTransaction().rollback();
			userinfoDAO.getSession().close();
			return false;
		}
		
		try{
			/*
			 * delete authority
			 */
			AdminroleDAO d3 = new AdminroleDAO();
			RoleDAO d2 = new RoleDAO();
			List OldAuth = this.getAuths(temp);
			Iterator iter = temp.getRoles().iterator();
			while(iter.hasNext())
			{
				
				Role admin2 = (Role) iter.next();
				AdminroleId aid = new AdminroleId();
				aid.setRole(admin2);
				aid.setUserinfo(temp);
				Adminrole ad = d3.findById(aid);
				if (!authority.contains(admin2.getRole().toString()))
				{
					d3.delete(ad);
				}
				
				
			}
			
			temp.setIsAdmin(isAdmin);
			
			
			
			
			for (int i = 0;authority != null && i < authority.size()  ; i++)
			{
				if(!OldAuth.contains(authority.get(i)))
				{
					AdminroleId aid = new AdminroleId();
					aid.setUserinfo(temp);
				
					Role role = d2.findById(Integer.parseInt((String)authority.get(i)));
					aid.setRole(role);
				
					Adminrole adminrole = new Adminrole();
					adminrole.setId(aid);
				
					d3.attachDirty(adminrole);
				}
			}
			
				
			
			userinfoDAO.attachDirty(temp);
			
			
			
		}
		catch(Exception e)
		{
			userinfoDAO.getSession().getTransaction().rollback();
			userinfoDAO.getSession().close();
			System.out.print(e.getMessage());
			e.printStackTrace();
			return false;
		}
		userinfoDAO.getSession().getTransaction().commit();
		userinfoDAO.getSession().close();
		
		return true;
	}

	public List searchUser(String userInfo) {
		UserinfoDAO dao = new UserinfoDAO();
		
		return dao.findAll(userInfo);
	}


	public List getAllUserByPage(String username, int pageSize, int pageNow) {
		UserinfoDAO dao = new UserinfoDAO();
			
		return dao.findAllByPage(username, pageSize, pageNow);
	}
}
